<template>
 <!-- $attrs来绑定继承的属性(自动排除掉props中声明的属性) -->
  <button class="dui-button" :class="classes" :disabled="disabled" v-bind="$attrs">
    <span v-if="loading" class="dui-loadingIndicator"></span>
    <slot />
  </button>
</template>

<script lang="ts">
import { computed } from 'vue';

export default {
  inheritAttrs: false, // 默认不在组件最外层绑定继承的属性
  props: {
    theme: {
      type: String,
      default: 'button',
    },
    size: {
      type: String,
      default: 'normal',
    },
    level: {
      type: String,
      default: 'default',
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: false,
    }
  },
  setup (props, context) {
    // const { size, ...rest } = context.attrs;
    const { theme, size, level } = props;
    const classes = computed(() => {
      return {
        [`dui-button-theme-${theme}`]: theme,
        [`dui-button-size-${size}`]: size,
        [`dui-button-level-${level}`]: level,
      };
    });
    return { classes };
  }
}
</script>

<style lang="scss">
  @import '../styles/util';

  $h: 32px;
  $radius: 4px;

  .dui-button {
    box-sizing: border-box;
    height: $h;
    padding: 0 12px;
    cursor: pointer;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    white-space: nowrap;
    background: white;
    color: $text-default;
    border: 1px solid $border-default;
    border-radius: $radius;
    box-shadow: 0 1px 0 fade-out(black, 0.95);

    & + & {
      margin-left: 8px;
    }

    &:focus {
      outline: none;
    }

    &::-moz-focus-inner {
      border: 0;
    }

    &:hover,
    &:focus {
      color: $brand-primary;
      border-color: $border-default;
    }

    &-theme-link {
      border-color: transparent;
      box-shadow: none;
      color: $brand-primary;

      &:hover, &:focus {
        color: lighten($brand-primary, 10%);
      }
    }

    &-theme-text {
      border-color: transparent;
      box-shadow: none;
      color: inherit;

      &:hover, &:focus {
        background: darken(white, 5%);
      }
    }

    &-size-big {
      font-size: $font-lg;
      height: 42px;
      padding: 0 16px !important;
    }

    &-size-normal {
      font-size: $font-md;
      height: 32px;
      padding: 0 8px !important;
    }

    &-size-small {
      font-size: $font-sm;
      height: 20px;
      padding: 0 4px !important;
    }

    &-level-main {
      background: $brand-primary;
      color: white;
      border-color: $brand-primary;

      &:hover,
      &:focus {
        background: darken($brand-primary, 10%);
        border-color: darken($brand-primary, 10%);
        color: white;
      }
    }

    &-level-danger {
      background: $brand-danger;
      border-color: $brand-danger;
      color: white;
      
      &:hover,
      &:focus {
        background: darken($brand-danger, 10%);
        border-color: darken($brand-danger, 10%);
        color: white;
      }
    }

    &-theme-link.dui-button-level-danger {
      border-color: transparent;
      box-shadow: none;
      color: $brand-danger;
      background: white;

      &:hover,
      &:focus {
        color: lighten($brand-danger, 10%);
        border-color: lighten($brand-danger, 10%);
      }
    }

    &-theme-text.dui-button-level-danger {
      border-color: transparent;
      box-shadow: none;
      color: inherit;
      color: $text-default;
      background: white;

      &:hover,
      &:focus {
        color: darken($brand-danger, 10%);
        border-color: $brand-danger;
        background: darken(white, 5%);
      }
    }

    &-theme-link.dui-button-level-main {
      border-color: transparent;
      box-shadow: none;
      color: $brand-primary;
      background: white;

      &:hover,
      &:focus {
        color: lighten($brand-primary, 10%);
        border-color: lighten($brand-primary, 10%);
      }
    }

    &-theme-text.dui-button-level-main {
      border-color: transparent;
      box-shadow: none;
      color: inherit;
      color: $text-default;
      background: white;

      &:hover,
      &:focus {
        color: darken($brand-primary, 10%);
        border-color: $brand-primary;
        background: darken(white, 5%);
      }
    }

    &[disabled] {
      cursor: not-allowed;
      color: $bg-disabled !important;
      &:hover {
        border-color: $bg-disabled;
      }
    }

    .dui-button-theme-link, &.dui-button-theme-text {
      &[disabled] {
        cursor: not-allowed;
        color: $bg-disabled;
      }
    }

    > .dui-loadingIndicator {
      width: 14px;
      height: 14px;
      display: inline-block;
      margin-right: 4px;
      border-radius: 8px; 
      border-color: $brand-primary $brand-primary $brand-primary transparent;
      border-style: solid;
      border-width: 2px;
      animation: dui-spin 1s infinite linear;
    }
  }

  @keyframes dui-spin {
    0%{transform: rotate(0deg)} 
    100%{transform: rotate(360deg)} 
  }
</style>